use std::fs;

use include_dir::{Dir, include_dir};
use crate::runner::CliArgs;
use crate::runner::init::init_git::init_git;
use crate::runner::init::init_template::init_template;

mod init_template;
mod init_git;

static TEMPLATE_DIR: Dir<'_> = include_dir!("src/template");

///执行初始化命令
pub fn runner_init(args: CliArgs) -> Result<(), String> {
    // docs_name: &str, is_skip: bool
    //判断name在本地是否有同名文件夹或文件
    if let Ok(_) = fs::metadata(&args.name) {
        return Err(format!("name已经存在: {}", &args.name));
    }
    //输出文件
    if let Err(e) = init_template(&TEMPLATE_DIR, &args.name) {
        return Err(format!("模板文件解析失败: {e}"));
    }
    //初始化git
    if let Err(e) = init_git(&args.name, &args.skip_git) {
        return Err(format!("git初始化失败: {e}"));
    };
    Ok(())
}
